﻿/*
 *  Copyright (c) 2014-Present, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant 
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

// For clarity, this sample has all code in the one file. In a real project, you'd put every
// class in a separate file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoFacWrapper;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using Senparc.Weixin.MP.CommonAPIs;
using ZL.WeChatCard.Entity;
using ZL.WeChatCard.Entity.EasyUse;
using ZL.WeChatCard.Entity.ViewModel;
using ZL.WeChatCard.Service;
using ZL.WeChatCard.Service.Common;
using ZL.WeChatCard.Service.InterFace;
using ZL.WeChatCard.Utility;

namespace ZLWeChatCard.Controllers
{
	public class PaymentController : BaseController
    {
        public PaymentController()
		{
			
		}

        public ActionResult Regesiter(string act, string contro)
        {
            RegesiterModel rm=new RegesiterModel();
            rm.openId = OpenID;
            rm.returnurl = contro + "/" + act;
            return View("Regesiter", rm);
        }

        public ActionResult Index()
		{
            var pservice = ClassFactory.GetInstance<IPaymentService>();
            AccInfo item = pservice.GetNo(OpenID);
            if (item != null)
            {
                return View("QrCode", item);
            }
            if (!item.IsSuccess) return View("QrCode", item);
            RegesiterModel rm = new RegesiterModel();
            rm.openId = OpenID;
            rm.returnurl = "Payment/Index";
            return View("Regesiter", rm);
        }

        [HttpPost]
        public ActionResult Regesiterform(RegesiterModel page)
        {
            if (page.validatecode != validatecode)
            {
                page.success = false;
                page.errormsg = "验证码不正确！";
                return Json(page);
            }
            if (!page.phone.IsHandset())
            {
                page.success = false;
                page.errormsg = "手机号不正确！";
                return Json(page);
            }
            var rservice = ClassFactory.GetInstance<IRegesiterService>();
            ResponseResultInfo response = rservice.SimpleRegesiter(page.openId, page.phone);
            if (!response.IsSuccess)
            {
                page.success = false;
                page.errormsg = response.Errormodel.desc;
                return Json(page);
            }
            page.success = true;
            return Json(page);
        }
     

        public ActionResult Success()
        {
            return View("Success", null);
        }

        public ActionResult Sendcode(string phone)
        {
            string code= SMSHelper.createRandom(true, 4);
            validatecode = code;
            SMSHelper.SendValidateCode(phone, code);
            return Content(code);
        }
    }
}
